package com.gxljc.bear.crawler.weixin;

import com.gxljc.commons.mongo.cli.BaseMongoRepository;
import com.gxljc.commons.util.StringUtil;
import com.gxljc.bear.crawler.base.BaseMongoTools;
import com.gxljc.bear.crawler.base.DolphinCrawlerConsts;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by Administrator on 2016/10/10.
 */
public class WeixinMongodbUtil {
    private static BaseMongoRepository repo;
    private final static Logger LOG = Logger.getLogger(WeixinMongodbUtil.class);

    //save data to mongodb
    public static void savePageMongodb(List<PageTable> tables, String tableName) throws Exception {
        if (CollectionUtils.isEmpty(tables)) return;
        for (PageTable table : tables) {
            savePageMongodb(table, tableName);
        }
    }

    //save data to mongodb
    public static void savePageMongodb(PageTable table, String tableName) throws Exception {
        Query query = new Query();
        query.addCriteria(Criteria.where(DolphinCrawlerConsts.MONGODB_DEFAULT_ID).is(table.pageId));
        Update update = new Update();
        Field[] fields = table.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Object value = fields[i].get(table);
            if (value == null) continue;
            if (value instanceof List) {
                if (CollectionUtils.isEmpty((List) value)) continue;
            }
            if (value instanceof String) {
                if (StringUtils.isEmpty((String) value)) continue;
            }
            update.set(fields[i].getName(), value);
        }
        BaseMongoRepository repo = BaseMongoTools.getInstance(DolphinCrawlerConsts.MongoDBName.WEIXIN.getName());
        repo.upsert(query, update, tableName);
    }

    //save data to mongodb
    public static void saveMongodbAccount(AccountTable table, String tableName) throws Exception {
        Query query = new Query();
        query.addCriteria(Criteria.where(DolphinCrawlerConsts.MONGODB_DEFAULT_ID).is(table.accountId));
        Update update = new Update();
        Field[] fields = table.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Object value = fields[i].get(table);
            if (value == null) continue;
            if (value instanceof List) {
                if (CollectionUtils.isEmpty((List) value)) continue;
            }
            if (value instanceof String) {
                if (StringUtils.isEmpty((String) value)) continue;
            }
            update.set(fields[i].getName(), value);
        }
        BaseMongoRepository repo = BaseMongoTools.getInstance(DolphinCrawlerConsts.MongoDBName.WEIXIN.getName());

        repo.upsert(query, update, tableName);
    }

    //delete data to mongodb
    public static void deletePageMongodb(Object id, String tableName) throws Exception {
        BaseMongoRepository repo = BaseMongoTools.getInstance(DolphinCrawlerConsts.MongoDBName.WEIXIN.getName());
        repo.removeById(id, tableName);
    }
}
